//
//  HotAskDetailsTableView.m
//  SHHCPhone
//
//  Created by Sheng long on 14-9-21.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HotAskDetailsTableView.h"
#import "HotAskDetailsCell.h"
#import "HotAskDetailsCellView.h"

@interface HotAskDetailsTableView()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_mainTable;
    HotAskDetailsCellView *_headView;
}

@property (nonatomic,retain) NSArray *dataArray;

@end

@implementation HotAskDetailsTableView

@synthesize dataDic,dataArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _mainTable = [[UITableView alloc] initWithFrame:self.bounds];
        [_mainTable setDelegate:self];
        [_mainTable setDataSource:self];
        [_mainTable setSeparatorColor:[UIColor clearColor]];
        [_mainTable setBackgroundColor:GET_COLOR(@"#f7f7f7")];
        [self addSubview:_mainTable];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dDic
{
    dataDic = dDic;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    for (NSDictionary *dic in [dataDic objectForKey:@"reply"]) {
        [array addObject:[NSMutableDictionary dictionaryWithDictionary:dic]];
    }
    self.dataArray = array;
    [self setHeadView];
    [_mainTable reloadData];
}

-(void)setHeadView
{
    if (!_headView) {
        _headView = [[HotAskDetailsCellView alloc] initWithFrame:CGRectMake(0, 0, CURRENT_CONTENT_WIDTH, [HotAskDetailsCellView getHeight:[dataDic valueForKey:@"content"] nick:[[dataDic objectForKey:@"user"] valueForKey:@"nickname"] width:CURRENT_CONTENT_WIDTH])];
        [_headView setBackgroundColor:[UIColor whiteColor]];
        NSDictionary *user = [dataDic objectForKey:@"user"];
        [_headView setHeadImage:[user valueForKey:@"headimg"] nick:[user valueForKey:@"nickname"] remark:[user valueForKey:@"pregnancyText"] answer:[dataDic valueForKey:@"content"]];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return _headView.frame.size.height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return _headView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.section];
    HotAskDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[HotAskDetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setContent:[dataArray objectAtIndex:indexPath.row] size:[tableView rectForRowAtIndexPath:indexPath].size];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [dataArray objectAtIndex:indexPath.row];
    if (![dic objectForKey:@"cellheight"]) {
        CGFloat y = [HotAskDetailsCellView getHeight:[dic valueForKey:@"content"] nick:[[dic objectForKey:@"user"] valueForKey:@"nickname"] width:CURRENT_CONTENT_WIDTH-CELL_BG_OUT_GAP*2-CELL_BG_IN_GAP*2];
        y+=(CELL_BG_OUT_GAP*2+CELL_BG_IN_GAP*2);
        [dic setObject:[NSNumber numberWithFloat:y] forKey:@"cellheight"];
    }
    return [[dic objectForKey:@"cellheight"] floatValue];
}

@end
